package com.itheima.controller;


import com.itheima.domain.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Map;

@Controller
@RequestMapping("/param")
public class ParamController {

    @RequestMapping("/test01")
    public String test01(@RequestParam(value = "username",defaultValue = "abc") String name, int age){
        System.out.println(name+","+age);
        return "success";
    }
    @RequestMapping("/test02")
    public String test02(User user){
        System.out.println(user);
        return "success";
    }

    @RequestMapping("/test03")
    public String test03(Date birthday){
        System.out.println(birthday);
        return "success";
    }

    /**
     * 携带数据的方式
     * 1.原生的servlet
     * 2.modelAndView--存入request作用域
     * 3.model       --存入request作用域
     * 4.map         --存入request作用域
     *
     * controller跳转controller
     * redirect:重定向  (客户端跳转)
     * forward: 请求包含(服务器跳转)
     */
    @RequestMapping("/test04")
    public String test04(HttpServletRequest req, HttpSession session){
        req.setAttribute("username" ,"admin");
        session.setAttribute("password","123456");
        return "redirect:/param/test06";
    }

    @RequestMapping("/test05")
    public ModelAndView test05(ModelAndView mv){
        mv.setViewName("main");
        mv.addObject("username","username");
        return mv;
    }

    @RequestMapping("/test06")
    public String test06(Model model){
        model.addAttribute("username","nihao");
        return "main";
    }

    @RequestMapping("/test07")
    public String test07(Map map){
        map.put("username","zhangfei");
        return "main";
    }

}
